package br.com.aquinoevasconcelos.servlets.administrador;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.modelos.Endereco;
import br.com.aquinoevasconcelos.user.UsuarioAdm;

/**
 * Servlet implementation class AtualizarAdministrador
 */
@WebServlet("/AtualizarAdministrador")
public class AtualizarAdministrador extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AtualizarAdministrador() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Fachada fachada = Fachada.getInstancia();
		UsuarioAdm adm = new UsuarioAdm();
		Endereco endereco = new Endereco();

		endereco.setCidade(request.getParameter("cidade"));
		endereco.setBairro(request.getParameter("bairro"));
		endereco.setCep(request.getParameter("cep"));
		endereco.setComplemento(request.getParameter("complemento"));
		endereco.setEstado(request.getParameter("estado"));
		endereco.setRua(request.getParameter("rua"));

		adm.setId(Integer.parseInt(request.getParameter("id")));
		adm.setNome(request.getParameter("nome"));
		adm.setEmail(request.getParameter("email"));
		adm.setLogin(request.getParameter("login"));
		adm.setSenha(request.getParameter("senha"));
		adm.setCnpj(request.getParameter("cnpj"));
		adm.setEndereco(endereco);

		adm.setTelefone(request.getParameter("telefone"));
		adm.setCelular1(request.getParameter("celular1"));
		adm.setCelular2(request.getParameter("celular2"));
		adm.setFuncao(request.getParameter("funcao"));

		try {

			fachada.atualizarADM(adm);

			RequestDispatcher rd = request.getRequestDispatcher("EncaminharPainelAdm?msg=sucesso");
			rd.forward(request, response);
		} catch (Exception e) {
			RequestDispatcher rd = request.getRequestDispatcher("../erro.jsp");
			rd.forward(request, response);
			e.printStackTrace();

		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

}
